<!-- 个人店铺·订单管理 -->
<template>
	<section>
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;text-align: right;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					    <el-button type="primary" class="search-btn"  @click="handleOrder()">
							查询订单
							<i class="el-icon-arrow-down el-icon--right"></i>
					    </el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="List" highlight-current-row v-loading="loading" border :cell-style="cellStyle" :header-cell-style="rowClass" style="width: 100%;">
			<el-table-column type="expand">
			  <template slot-scope="props">
			    <el-form label-position="left" inline class="demo-table-expand">
					<el-form-item label="商品 ID:">
						<span>{{ props.row.goodsId }}</span>
					</el-form-item>
					<el-form-item label="收货人姓名:">
						<span>{{ props.row.recvName }}</span>
					</el-form-item>
					<el-form-item label="收货人手机号:">
						<span>{{ props.row.recvMobile }}</span>
					</el-form-item>
					<el-form-item label="收货人地址:">
						<span>{{ props.row.recvAddress }}</span>
					</el-form-item>
					<el-form-item label="评价内容:">
						<span>{{ props.row.evalContent }}</span>
					</el-form-item>
					<el-form-item label="评价时间:">
						<span>{{ props.row.evalTime }}</span>
					</el-form-item>
					<el-form-item label="物流签收时间:">
						<span>{{ props.row.receiveTime }}</span>
					</el-form-item>
					<el-form-item label="柚子释放时间:">
						<span>{{ props.row.releaseTime }}</span>
					</el-form-item>
					<el-form-item label="备注:">
						<span>{{ props.row.reason }}</span>
					</el-form-item>
			    </el-form>
			  </template>
			</el-table-column>
			<el-table-column prop="id" align="center" label="ID" width="60"></el-table-column>
			<el-table-column prop="orderNo" align="center" label="订单号" width="200"></el-table-column>
			<el-table-column prop="goodsName" align="center" label="商品名称" width="200"></el-table-column>
			<el-table-column prop="specName" align="center" label="商品规格" width="100"></el-table-column>
			<el-table-column prop="orderNumber" align="center" label="购买数量" ></el-table-column>
			<el-table-column prop="orderPrice" align="center" label="商品价格" ></el-table-column>
			<el-table-column prop="orderRealPrice" align="center" label="实付价格" ></el-table-column>
			<el-table-column prop="orderStatus" align="center" label="订单状态">
				<template slot-scope="scope">
					<p v-if="scope.row.orderStatus == '0'">待支付</p>
					<p v-else-if="scope.row.orderStatus == '1'">已支付</p>
					<p v-else-if="scope.row.orderStatus == '2'">待收货</p>
					<p v-else-if="scope.row.orderStatus == '3'">已签收</p>
					<p v-else-if="scope.row.orderStatus == '4'">待评价</p>
					<p v-else-if="scope.row.orderStatus == '5'">已完成</p>
					<p v-else-if="scope.row.orderStatus == '6'">取消</p>
				</template>
			</el-table-column>
			<el-table-column prop="expressNumber" align="center" label="物流单号"></el-table-column>
			<el-table-column prop="createTime" align="center" label="创建时间" width="170"></el-table-column>
			<el-table-column label="操作" align="center" width="140" >
				<template slot-scope="scope">
					<el-button v-if="scope.row.orderStatus != '5'" size="small" type="warning" icon="el-icon-edit" @click="handleEdit(scope.$index,scope.row)">修改状态</el-button>
					<el-button v-else size="small" type="info" icon="el-icon-edit" >修改状态</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--分页-->
		<el-col :span="24" class="toolbar">
			<el-pagination background layout="total,sizes,prev, pager, next,jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!-- 修改订单状态-->
		<el-dialog title="修改订单状态" :visible.sync="dialogVisible" width="35%" :before-close="handleClose">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="修改原因" prop="reason">
					<el-input v-model="editForm.reason" auto-complete="off" placeholder="请输入操作订单的原因"></el-input>
				</el-form-item>
				<el-form-item label="修改状态" prop="orderStatus">
					<el-radio-group v-model="editForm.orderStatus">
						<el-radio class="radio" :label="5">已完成</el-radio>
						<el-radio class="radio" :label="6">取消</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary"  @click.native="addSubmit" >确定</el-button>
			</span>
		</el-dialog>
		<!-- 自定义查询 cusexcel-->
		<el-dialog title="自定义查询订单" :visible.sync="cusdialog" width="35%" :before-close="handleClose">
			<el-form :model="cusForm" label-width="105px" ref="cusForm">
				<el-form-item label="订单ID" prop="orderId">
					<el-input v-model="cusForm.orderId" auto-complete="off" placeholder="请输入正确的订单ID" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="订单单号" prop="orderNo">
					<el-input v-model="cusForm.orderNo" auto-complete="off" placeholder="请输入正确的订单单号" style="width: 220px;"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="cusdialog = false">取 消</el-button>
				<el-button type="primary"  @click.native="cusSubmit" >查询</el-button>
			</span>
		</el-dialog>
	</section>
</template>
<script>
	import {
		getGoodsOrderList,
		editOrderStatus,
		Refreshtoken
	} from '../../../api/api';

	export default {
		data() {
			return {
				radio: '',
				filters: {},
				loading: false,
				List: [],
				type:0,
				total: 0,
				page: 1,
				size:10,
				sels: [],//列表选中列
				timer: null,
				dialogVisible:false,
				editForm:{
					reason:'',
					orderStatus:5
				},
				editFormRules:{
					reason: [{
						required: true,
						message: '请输入修改订单的原因',
						trigger: 'blur'
					}],
					orderStatus: [{
						required: true,
						message: '请选择操作订单的状态',
						trigger: 'blur'
					}]
				},
				cusdialog:false,
				cusForm:{
					orderId:'',
					orderNo:''
				},
			}
		},
		mounted() {
			var Token = sessionStorage.getItem('Token');
			var timestamp=new Date().getTime();
			let para = {
				token: Token,
				curpage:this.page,
				pagesize:this.size,
				timestamp: timestamp
			};
			this.loading = true;
			getGoodsOrderList(para).then((res) => {
				var res = res.data;
				if (res.code != 0) {
					this.$message({
						message: res.message,
						type: 'error'
					});
				} else {
					this.total = res.data.total;
					this.List = res.data.list;
					this.loading = false;
				}
			});
			
			this.queryInfo();
			this.timer = setInterval(() => {
			   setTimeout(this.queryInfo, 0)
		    }, 600000)
			
		},
		methods: {
			// 设置定时器
			queryInfo() {
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {token: Token,timestamp: timestamp};
				Refreshtoken(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.loading = false;
					}
				});
			},
			// 表格样式
			cellStyle({row, column}){ return 'text-align:center;padding:0;'; },
			rowClass({row, column}){ return 'text-align:center'; },
			//分页查询
			handleSizeChange(val) {
				this.size = val;
				this.getUsers();
			},
			handleCurrentChange(currentPage) {
				this.page = currentPage;
				this.getUsers();
			},
			// 关闭状态
			handleClose(done) {
				this.$confirm('确认关闭？')
				.then(_ => {
					done();
				})
				.catch(_ => {});
			},
			// 修改
			handleEdit(index,row){
				this.dialogVisible = true;
				this.editForm = Object.assign({}, row);
				this.pid = row.id
			},
			addSubmit: function () {
				if(this.editForm.orderStatus == '6'){
					this.$confirm('取消订单将直接影响交易双方柚子数，请慎重选择?', '警告', {
					  confirmButtonText: '确定',
					  type: 'warning'
					}).then(() => {
						this.upsubmit()
					}).catch(() => {
					  this.$message({
						type: 'info',
						message: '已取消'
					  });          
					});
				}else if(this.editForm.orderStatus == '5'){
					this.$confirm('确认修改订单状态吗？', '提示', {}).then(() => {
						this.dialogVisible = true;
						var Token = sessionStorage.getItem('Token');
						var timestamp = new Date().getTime();
						let para = {token: Token,id:this.pid,reason:this.editForm.reason,timestamp: timestamp,orderStatus:this.editForm.orderStatus};
						editOrderStatus(para).then((res) => {
							var res = res.data;
							if (res.code != 0) {
								this.$message({
									message: res.message,
									type: 'error'
								});
							} else {
								this.$message({
									type: 'success',
									message: '订单状态修改成功！！！'
								});
								this.$refs['editForm'].resetFields();
								this.dialogVisible = false;
								this.loading = false;
								this.getUsers();
							}
						});
					});
				}
			},
			upsubmit(){
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认修改订单状态吗？', '提示', {}).then(() => {
							this.dialogVisible = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp = new Date().getTime();
							let para = {token: Token,id:this.pid,reason:this.editForm.reason,timestamp: timestamp,orderStatus:this.editForm.orderStatus};
							editOrderStatus(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								} else {
									this.$message({
										type: 'success',
										message: '订单状态修改成功！！！'
									});
									this.$refs['editForm'].resetFields();
									this.dialogVisible = false;
									this.loading = false;
									this.getUsers();
								}
							});
						});
					}
				})
			},
			
			//查询订单
			handleOrder(){
				this.cusdialog = true;
			},
			cusSubmit(){
				this.$refs.cusForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认查询订单吗？', '提示', {}).then(() => {
							this.cusdialog = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp = new Date().getTime();
							if (this.cusForm.orderId != '') {
								var para = {token: Token,timestamp: timestamp,curpage:this.page,pagesize:this.size,orderId:this.cusForm.orderId};
							}else if(this.cusForm.orderNo != ''){
								var para = {token: Token,timestamp: timestamp,curpage:this.page,pagesize:this.size,orderNo:this.cusForm.orderNo};
							}
							getGoodsOrderList(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								} else {
									this.total = res.data.total;
									this.List = res.data.list;
									this.$refs['cusForm'].resetFields();
									this.cusdialog = false;
									this.loading = false;
								}
							});
						});
					}
				})
			},
			//获取用户列表 判断用户是否在那个列表中
			getUsers: function(type) {
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {
					token: Token,
					curpage:this.page,
					pagesize:this.size,
					expressState:type,
					timestamp: timestamp
				};
				this.loading = true;
				getGoodsOrderList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.total = res.data.total;
						this.List = res.data.list;
						this.loading = false;
					}
				});
			},
			
		},
		beforeDestroy() {
		  clearInterval(this.timer);        
		  this.timer = null;
		}
	};
</script>

<style scoped>
.el-button.is-disabled{
	color: #bfcbd9;
	    cursor: not-allowed;
	    background-image: none;
	    background-color: #eef1f6;
	    border-color: #d1dbe5;
}
.el-pagination {
    white-space: nowrap;
    padding: 2px 5px;
    color: #48576a;
}
.demo-table-expand {
	display: flex;
	flex-direction: column;
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
